﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

using Mortgage.DAL.Entity;
using Mortgage.DAL;


namespace Mortgage.Srv
{
    public interface IAgentSrv
    {
        List<tAgent> GetAll();
        tAgent GetById(int id);
        int Insert(tAgent insObj);
        int Update(tAgent newsObj);
        int Delete(tAgent delObje);
        bool CanDelete(tAgent delObje);
    }

    class AgentSrv : IAgentSrv
    {
        const int PAGE_SIZE = 50;
        private MortgageDataContext dataContext;

        public AgentSrv()
        {
            dataContext = new MortgageDataContext();

        }


        public List<tAgent> GetAll()
        {
            List<tAgent> result;
            result = dataContext.tAgents.Where(m => !m.IsDelete.GetValueOrDefault(false)).OrderBy(a => a.CreatedDate).ToList();
            return result;
        }

        public tAgent GetById(int id)
        {
            tAgent result = dataContext.tAgents.Where(a => a.ID == id).FirstOrDefault();
            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="insObj"></param>
        /// <returns>
        /// 0: success
        /// other: error code
        /// </returns>
        public int Insert(tAgent insObj)
        {
            insObj.IsDelete = false;
            dataContext.tAgents.InsertOnSubmit(insObj);
            dataContext.SubmitChanges();
            return 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="newsObj"></param>
        /// <returns>
        /// 0: success
        /// 1: No find update object
        /// other: error code
        /// </returns>
        public int Update(tAgent newsObj)
        {
            tAgent entity = dataContext.tAgents.Where(a => a.ID == newsObj.ID).FirstOrDefault();
            if (entity == null) return 1;

            entity.ModifiedBy = newsObj.ModifiedBy;
            entity.ModifiedDate = newsObj.ModifiedDate;

            entity.Name = newsObj.Name;
            entity.Address = newsObj.Address;
            entity.Telephone = newsObj.Telephone;
            entity.Description = newsObj.Description;

            dataContext.SubmitChanges();
            return 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="delObje"></param>
        /// <returns>
        /// 0: succcessful
        /// 1: No find delete object
        /// other: error code
        /// </returns>
        public int Delete(tAgent delObje)
        {
            tAgent entity = dataContext.tAgents.Where(a => a.ID == delObje.ID).FirstOrDefault();
            if (entity == null) return 1;

            if (dataContext.tMortgages.Any(m => m.Age_ID == delObje.ID) || dataContext.tInterestRates.Any(m => m.Age_ID == delObje.ID) || dataContext.tBills.Any(m => m.Age_ID == delObje.ID))
            {
                entity.IsDelete = true;
            }
            else
                dataContext.tAgents.DeleteOnSubmit(entity);
            dataContext.SubmitChanges();

            return 0;
        }
        public bool CanDelete(tAgent delObje)
        {
            if (!dataContext.tMortgages.Any(m => m.Age_ID == delObje.ID)) return true;
            if (!dataContext.tBills.Any(m => m.Age_ID == delObje.ID)) return true;
            if (!dataContext.tInterestRates.Any(m => m.Age_ID == delObje.ID)) return true;
            return false;

        }
    }
}
